package com.dinglian.ots.controller.buyer;

import com.dinglian.ots.common.model.Constant;
import com.dinglian.ots.dao.model.User;
import com.dinglian.ots.exception.GlobalException;
import com.dinglian.ots.result.CodeMsg;
import com.dinglian.ots.result.Result;
import com.dinglian.ots.service.BuyerService;
import com.dinglian.ots.shiro.MyShiroToken;
import com.dinglian.ots.vo.address.AddAddressVo;
import com.dinglian.ots.vo.address.AddressListVo;
import com.dinglian.ots.vo.address.UpdateAddressVo;
import com.dinglian.ots.vo.brand.AgreeVo;
import com.dinglian.ots.vo.buyer.*;
import com.dinglian.ots.vo.collection.CollectionVo;
import com.dinglian.ots.vo.user.UserVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @author wangbo
 */
@Slf4j
@RestController
@Api(value = "/buyer", tags = "买手模块")
@RequestMapping("/buyer/")
@CrossOrigin
public class BuyerController {

    @Autowired
    private BuyerService buyerService;

    @ApiOperation("买手注册")
    @PostMapping("buyerRegister")
    public Result buyerRegister(@Validated BuyerVo buyerVo) {
        UserVo userVo = new UserVo();
        try {
            User user = buyerService.saveBuyer(buyerVo);
            Subject subject = SecurityUtils.getSubject();
            MyShiroToken token = new MyShiroToken(user.getUsername(), user.getPassword(), Constant.BUYER_ROLE);
            subject.login(token);
            userVo = (UserVo) subject.getPrincipal();
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(userVo);
    }

    @ApiOperation(value = "获取买手资料")
    @RequiresRoles({"by"})
    @GetMapping("getBuyerInfo")
    public Result getBuyerInfo(@RequestParam("storeId") Integer storeId) {
        BuyerInfoVo buyerInfoVo = new BuyerInfoVo();
        try {
            buyerInfoVo = buyerService.getBuyerInfo(storeId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(buyerInfoVo);
    }


    @RequiresRoles({"by"})
    @ApiOperation(value = "更新买手资料")
    @PostMapping("updateBuyerInfo")
    public Result updateBuyerInfo( UpdateBuyerVo buyerVo) {
        try {
            buyerService.updateBuyerInfo(buyerVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }


    @RequiresRoles({"by"})
    @ApiOperation(value = "收藏", notes = "type 1-店铺  2-品牌 3-产品")
    @PostMapping("buyerCollect")
    public Result buyerCollect(@RequestBody @Validated CollectionVo collectionVo) {

        try {
            buyerService.buyerCollect(collectionVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }


    @RequiresRoles({"by"})
    @ApiOperation(value = "添加收货地址", notes = "prime 0-不是默认地址 1-是默认地址")
    @PostMapping("addAddress")
    public Result addAddress(@RequestBody @Validated AddAddressVo addressVo) {

        try {
            buyerService.addAddress(addressVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @RequiresRoles({"by"})
    @ApiOperation(value = "修改收货地址", notes = "prime 0-不是默认地址 1-是默认地址")
    @PostMapping("updateAddress")
    public Result updateAddress(@RequestBody @Validated UpdateAddressVo addressVo) {

        try {
            buyerService.updateAddress(addressVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @RequiresRoles({"by"})
    @ApiOperation(value = "删除收货地址")
    @PostMapping("deleteAddress/{addressId}")
    public Result deleteAddress(@PathVariable("addressId") Integer addressId) {

        try {
            buyerService.deleteAddress(addressId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }


    @RequiresRoles({"by"})
    @ApiOperation(value = "收货地址列表")
    @GetMapping("getAddressList")
    public Result getAddressList(@RequestParam("userId") Integer userId) {
        List<AddressListVo> addressList = new ArrayList<>();
        try {
            addressList = buyerService.getAddressList(userId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }


    @ApiOperation(value = "买手意向申请")
    @PostMapping("applyForIntention")
    public Result applyForIntention(@RequestBody ApplicationVo applicationVo) {

        try {
            buyerService.applyForIntention(applicationVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @ApiOperation("邀请通过/驳回")
    @GetMapping("agreeInvitation")
    public String agreeInvitation(@RequestParam(value = "relationId") Integer relationId,
                                 @RequestParam(value = "isPass") boolean isPass) {
        AgreeVo agreeVo = new AgreeVo();
        agreeVo.setPass(isPass);
        agreeVo.setRelationId(relationId);
        buyerService.agreeInvitation(agreeVo);
        return CodeMsg.SUCCESS2.getMsg();
    }


    @ApiOperation(value = "搜索买手")
    @GetMapping("searchBuyer")
    public Result searchBuyer(@RequestParam("brandId") Integer brandId,
                              @RequestParam(value = "province", required = false) Integer provinceId,
                              @RequestParam(value = "cityId", required = false) Integer cityId,
                              @RequestParam(value = "styleId", required = false) String styleIds,
                              @RequestParam(value = "type", required = false) String type,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        PageInfo<SearchBuyerVo> searchBuyerVos = new PageInfo<>();
        try {
            searchBuyerVos =  buyerService.searchBuyer(brandId,provinceId,cityId,styleIds,type,pageNum,pageSize);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(searchBuyerVos);
    }


}
